package com.lovvyang.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

/**
 * @author lovvyang
 * @date 2020/6/8-13:04
 */
@WebServlet("/RequestDemo02")
public class RequestDemo02 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //中文乱码
        request.setCharacterEncoding("utf-8");
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            String value = request.getHeader(name);
            System.out.println(name+"---"+value);

        }
        System.out.println("-------------");
        BufferedReader reader = request.getReader();
        String line=null;
        while ((line=reader.readLine())!=null){
            System.out.println(line);
        }
        System.out.println("-------------");
        //获取请求参数
        String username = request.getParameter("username");
        System.out.println(username);
        System.out.println("-------------");
        /*Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()){
            String nextElement = parameterNames.nextElement();
            System.out.println(nextElement);
            String parameter = request.getParameter(nextElement);
            System.out.println(parameter);
            System.out.println("--------------");
        }*/
        //请求转发
        //共享数据
        //获取ServletContext
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);

    }
}
